package com.wzcl.app.controller;

import com.wzcl.app.dao.model.CourseMaterial;
import com.wzcl.app.model.common.MyPage;
import com.wzcl.app.model.common.RVO;
import com.wzcl.app.model.dto.CourseMaterialDto;
import com.wzcl.app.model.vo.CourseMaterialVo;
import com.wzcl.app.service.CourseMaterialService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

@Log4j2
@RestController
@RequestMapping("/courseMaterial")
@Api(value="课程教材管理controller",tags={"课程教材管理接口"})
public class CourseMaterialController {

    @Autowired
    private CourseMaterialService service;

    @ApiOperation("添加课程教材")
    @PostMapping("/add")
    public RVO<CourseMaterialVo> addCourseMaterial(@RequestBody @Valid CourseMaterialDto courseMaterialDto){
        CourseMaterial courseMaterial = service.addCourseMaterial(courseMaterialDto);
        return RVO.success(CourseMaterialVo.create(courseMaterial));
    }


    @ApiOperation("删除课程教材")
    @PostMapping("/del")
    public RVO<CourseMaterialVo> delCourseMaterial(@RequestBody CourseMaterialDto courseMaterialDto){
        CourseMaterial courseMaterial = service.delCourseMaterialById(courseMaterialDto.getId());
        return RVO.success(CourseMaterialVo.create(courseMaterial));
    }


    @ApiOperation("修改课程教材")
    @PostMapping("/update")
    public RVO<CourseMaterialVo> updateCourseMaterial(@RequestBody @Valid CourseMaterialDto courseMaterialDto){
        CourseMaterial courseMaterial = service.updateCourseMaterial(courseMaterialDto);
        return RVO.success(CourseMaterialVo.create(courseMaterial));
    }


    @ApiOperation("获取课程教材")
    @PostMapping("/get")
    public RVO<CourseMaterialVo> getCourseMaterial(@RequestBody CourseMaterialDto courseMaterialDto){
        CourseMaterial courseMaterial = service.getCourseMaterialById(courseMaterialDto.getId());
        if(courseMaterial !=null){
            return RVO.success(CourseMaterialVo.create(courseMaterial));
        }
        return RVO.success(null);
    }

    @ApiOperation("课程教材列表list")
    @PostMapping("/getList")
    public RVO<List<CourseMaterialVo>> getCourseMaterialList(@RequestBody CourseMaterialDto courseMaterialDto){
        List<CourseMaterial> list = service.getCourseMaterialList(courseMaterialDto);
        if (list != null && list.size() > 0) {
            List<CourseMaterialVo> collect = list.stream().map(CourseMaterialVo::create).collect(Collectors.toList());
            return RVO.success(collect);
        }
        return null;
    }

    @ApiOperation("课程教材列表page")
    @PostMapping("/getPage")
    public  RVO<List<CourseMaterialVo>> getCourseMaterialPage(@RequestBody CourseMaterialDto courseMaterialDto){
        MyPage<CourseMaterial> page = service.getCourseMaterialPage(courseMaterialDto);
        if (page != null && page.getContent().size()>0) {
            List<CourseMaterialVo> collect = page.getContent().stream().map(CourseMaterialVo::create).collect(Collectors.toList());
            return RVO.success(collect,page);
        }
        return null;
    }


}
